package com.xiaoyu.lanling.feature.chat.viewholder;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.cardview.widget.CardView;

import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
import com.xiaoyu.base.app.AppContext;
import com.xiaoyu.lanling.R;
import com.xiaoyu.lanling.feature.chat.model.message.ChatMessageReceiveVideoItem;
import com.xiaoyu.lanling.feature.chat.viewholder.messageclick.ChatMessageAvatarItemClickHandler;
import com.xiaoyu.lanling.feature.chat.viewholder.messageclick.ChatMessageVideoItemClickHandler;
import com.xiaoyu.lanling.view.UserAvatarDraweeView;
import com.xiaoyu.lanling.view.text.UserNameTextView;
import com.xiaoyu.lanling.widget.player.MessageVideoPlayer;

import org.jetbrains.annotations.NotNull;

public class ChatMessageReceiveVideoViewHolder extends ChatMessageBaseReceiveSendViewHolder<ChatMessageReceiveVideoItem> {

    private static final String TAG = ChatMessageReceiveVideoItem.class.getSimpleName();

    private GSYVideoOptionBuilder mVideoOptionBuilder;

    private View mView;
    private UserAvatarDraweeView mUserAvatar;
    private UserNameTextView mUserDisplayName;
    private CardView mCardView;
    private MessageVideoPlayer mVideoPlayer;

    @Override
    protected View doCreateChildrenView(LayoutInflater layoutInflater, ViewGroup parent) {
        this.mView = layoutInflater.inflate(R.layout.item_chat_receive_video, parent, false);
        this.mUserAvatar = mView.findViewById(R.id.chat_message_avatar);
        this.mUserDisplayName = mView.findViewById(R.id.chat_message_name);
        this.mCardView = mView.findViewById(R.id.chat_message_video_card_layout);
        this.mVideoPlayer = mView.findViewById(R.id.chat_message_video_player);
        this.mVideoOptionBuilder = new GSYVideoOptionBuilder();
        return mView;
    }

    @Override
    public void showData(int position, @NotNull ChatMessageReceiveVideoItem itemData) {
        super.showData(position, itemData);
        final ChatMessageReceiveVideoItem chatMessageReceiveVideoItem = itemData;
        if (chatMessageReceiveVideoItem.isMultiChat()) {
            mUserDisplayName.setVisibility(View.VISIBLE);
            mUserDisplayName.setText(chatMessageReceiveVideoItem.getDisplayName());
        } else {
            mUserDisplayName.setVisibility(View.GONE);
        }
        mCardView.getLayoutParams().width = chatMessageReceiveVideoItem.getWidth();
        mCardView.getLayoutParams().height = chatMessageReceiveVideoItem.getHeight();
        mCardView.requestLayout();

        initVideoPlayer(chatMessageReceiveVideoItem);
        ChatMessageVideoItemClickHandler.bindReceiveVideoItemLongClickForViewAndData(mVideoPlayer, chatMessageReceiveVideoItem);
        ChatMessageAvatarItemClickHandler.bindReceiveImageItemLongClickForViewAndData(mUserAvatar, chatMessageReceiveVideoItem);
    }

    private void initVideoPlayer(ChatMessageReceiveVideoItem item) {
        String url = item.getVideoUrl();
        if (url.startsWith("https")) {
            url = url.replace("https", "http");
        }
        mVideoOptionBuilder
                .setIsTouchWiget(false)
                .setUrl(url)
                .setVideoTitle("")
                .setIsTouchWigetFull(false)
                .setCacheWithPlay(true)
                .setRotateViewAuto(false)
                .setLockLand(false)
                .setPlayTag(TAG)
                .setLooping(false)
                .setShowFullAnimation(true)
                .setNeedLockFull(false)
                .setPlayPosition(mPosition)
                .setNeedShowWifiTip(false)
                .setThumbPlay(true)
                .setBottomProgressBarDrawable(AppContext.getDrawable(R.drawable.layer_video_progress))
                .setBottomShowProgressBarDrawable(AppContext.getDrawable(R.drawable.layer_video_progress), AppContext.getDrawable(R.drawable.selector_video_thump))
                .setVideoAllCallBack(new GSYSampleCallBack() {
                    @Override
                    public void onClickStartIcon(String url, Object... objects) {
                    }

                    @Override
                    public void onPrepared(String url, Object... objects) {
                        super.onPrepared(url, objects);
                        if (!mVideoPlayer.isIfCurrentIsFullscreen()) {
                            GSYVideoManager.instance().setNeedMute(true);
                        }
                    }

                    @Override
                    public void onQuitFullscreen(String url, Object... objects) {
                        super.onQuitFullscreen(url, objects);
                        GSYVideoManager.instance().setNeedMute(true);
                    }

                    @Override
                    public void onEnterFullscreen(String url, Object... objects) {
                        super.onEnterFullscreen(url, objects);
                        GSYVideoManager.instance().setNeedMute(false);
                    }
                }).build(mVideoPlayer);

        mVideoPlayer.bindReceiveVideoItemViewAndDataForLongClick(item);
        mVideoPlayer.setDuration(item.getDuration());
        mVideoPlayer.loadCoverImage(item.getFirstPicture(), item.getWidth(), item.getHeight());
        mVideoPlayer.getTitleTextView().setVisibility(View.GONE);
        mVideoPlayer.getBackButton().setVisibility(View.GONE);
    }
}
